package com.kylin.gateway.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
import com.kylin.gateway.handler.SentinelFallbackHandler;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
 * 网关配置
 * @author wuhao
 */
@Configuration
public class GatewayConfig
{
    /**
     * 限流处理器
     * @return
     * @author wuhao
     * @since 2.6.0 - 2021/10/13
     */
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public SentinelFallbackHandler sentinelGatewayExceptionHandler()
    {
        return new SentinelFallbackHandler();
    }


    @Bean
    @Order(-1)
    public GlobalFilter sentinelGatewayFilter()
    {
        return new SentinelGatewayFilter();
    }

    /**
     * 跨域处理
     * @return
     * @author wuhao
     * @since 2.6.0 - 2022/10/13
     */
//    @Bean
//    public CorsWebFilter corsWebFilter(){
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//
//        CorsConfiguration corsConfiguration = new CorsConfiguration();
//
//        //1、配置跨域
//        // 允许任何头
//        corsConfiguration.addAllowedHeader("*");
//        // 允许任何方法（post、get等）
//        corsConfiguration.addAllowedMethod("*");
//        // 允许任何域名使用
//        corsConfiguration.addAllowedOrigin("*");
//        //允许携带cookie访问
//        corsConfiguration.setAllowCredentials(true);
//
//        source.registerCorsConfiguration("/**",corsConfiguration);
//        return new CorsWebFilter(source);
//    }
}